#include"WorkerManager.h"

WorkerManager::WorkerManager()
{
	
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	//文件不存在
	if (!ifs.is_open())
	{
		//cout << "文件不存在。" << endl;

		this->m_EmpNum = 0;

		this->m_EmpArray = NULL;

		this->m_FileIsEmpty = true;

		ifs.close();
		return;
	}

	//文件存在，为空
	char ch;
	ifs >> ch;		//读取文件第一个字符，若该字符为eof(eof为文件尾部标志)
	if (ifs.eof())	//那么该文件为空, eof()函数返回值为真
	{
		//cout << "文件为空。" << endl;

		this->m_EmpNum = 0;
		this->m_EmpArray = NULL;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}


	//文件存在，不为空
	//初始化 m_EmpNum
	int num = this->get_EmpNum();
	//cout << "职工人数为: " << num << endl;
	this->m_EmpNum = num;

	//初始化 m_EmpArray
	//开辟空间
	this->m_EmpArray = new Worker * [this->m_EmpNum];
	//将文件中的数据存入数组
	this->init_EmpArray();
}


void WorkerManager::show_Menu()
{
	cout << "******************************************" << endl;
	cout << "******************************************" << endl;
	cout << "********** 欢迎使用职工管理系统 **********" << endl;
	cout << "**********    0.退出管理程序    **********" << endl;
	cout << "**********    1.增加职工信息    **********" << endl;
	cout << "**********    2.显示职工信息    **********" << endl;
	cout << "**********    3.删除离职职工    **********" << endl;
	cout << "**********    4.修改职工信息    **********" << endl;
	cout << "**********    5.查找职工信息    **********" << endl;
	cout << "**********    6.按照编号排序    **********" << endl;
	cout << "**********    7.清空所有编号    **********" << endl;
	cout << "******************************************" << endl;
	cout << endl;

}


void WorkerManager::exitSystem()
{
	cout << "欢迎下次使用!" << endl;
	system("pause");
	exit(0);	//在main以外的函数终止
}


void WorkerManager::save()
{
	ofstream ofs;

	ofs.open(FILENAME, ios::out);

	for (int i = 0; i < this->m_EmpNum; i++)
	{
		ofs << this->m_EmpArray[i]->m_ID << "  "
			<< this->m_EmpArray[i]->m_Name << "  "
			<< this->m_EmpArray[i]->m_DeptID << endl;

	}

	ofs.close();
}


void WorkerManager::Add_Emp()
{
	cout << "请输入添加职工数量: " << endl;
	int addNum = 0;
	cin >> addNum;

	if (addNum > 0)
	{
		//计算添加新空间的大小
		int newSize = this->m_EmpNum + addNum;

		///开辟新空间
		Worker** newSpace = new Worker * [newSize];

		//将原来空间下数据拷贝到新空间
		if (this->m_EmpArray != NULL)
		{
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				newSpace[i] = this->m_EmpArray[i];
			}
		}

		//批量添加新数据
		for (int i = 0; i < addNum; i++)
		{
			int id;
			string name;
			int dSelect;	//部门选择

			cout << "请输入第" << i + 1 << "个新职工编号: " << endl;
			cin >> id;

			cout << "请输入第" << i + 1 << "个新职工姓名: " << endl;
			cin >> name;

			cout << "请选择职工岗位: " << endl;
			cout << "1.普通职工 " << endl;
			cout << "2.经理 " << endl;
			cout << "3.老板 " << endl;
			cin >> dSelect;

			Worker* worker = NULL;
			switch (dSelect)
			{
			case 1:
				worker = new Employee(id, name, 1);
				break;

			case 2:
				worker = new Manager(id, name, 2);
				break;

			case 3:
				worker = new Boss(id, name, 3);
				break;

			default:
				break;
			}
			//将创建职工职责保存到数组中
			newSpace[this->m_EmpNum + i] = worker;
		}

		//释放原有空间
		delete[] this->m_EmpArray;

		//更改新空间的指向
		this->m_EmpArray = newSpace;

		//更新新的职工人数
		this->m_EmpNum = newSize;

		//成功添加后保存到文件中
		this->save();

		this->m_FileIsEmpty = false;

		cout << "成功添加" << addNum << "名新职工!" << endl;
	}
	else
		cout << "输入数据有误。" << endl;

	system("pause");
	system("cls");
}


int WorkerManager::get_EmpNum()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int id;
	string name;
	int dID;

	int num = 0;
	while (ifs >> id && ifs >> name && ifs >> dID)
	{
		//统计人数
		num++;
	}
	return num;
}


void WorkerManager::init_EmpArray()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int id; 
	string name;
	int dID;

	int index = 0;
	while (ifs >> id && ifs >> name && ifs >> dID)
	{
		Worker* worker = NULL;

		//根据不同的部门id创建不同对象
		if (dID == 1)		//员工
		{
			worker = new Employee(id, name, dID);
		}
		else if (dID == 2)	//经理
		{
			worker = new Manager(id, name, dID);
		}
		else				//老板 
		{
			worker = new Boss(id, name, dID);
		}
		
		this->m_EmpArray[index] = worker;
		index++;

	}
	ifs.close();
}


void WorkerManager::Show_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "该文件为空或不存在！" << endl;
	}
	else
	{
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			this->m_EmpArray[i]->showInfo();
		}
	}

	system("pause");
	system("cls" );
}


int WorkerManager::IsExist(int id)
{
	for (int i = 0; i < this->m_EmpNum; i++)
	{
		if (id == this->m_EmpArray[i]->m_ID)
		{
			return i ;
		}
	}
	return -1;
}

void WorkerManager::Del_Emp()
{
	cout << "请输入需要删除职工的编号: " << endl;
	int del_id;
	cin >> del_id;
	int index = this->IsExist(del_id);
	if (index == -1)
	{
		cout << "该职工不存在。" << endl;
	}
	else
	{
		for (int j = index; j < this->m_EmpNum - 1; j++)
		{
			this->m_EmpArray[j] = this->m_EmpArray[j + 1];
		}
		(this->m_EmpNum)--;

		this->save();
		cout << "删除成功。" << endl;
	}
	system("pause");
	system("cls");
}


void WorkerManager::Mod_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或为空。" << endl;
	}
	else
	{
		cout << "请输入修改职工的编号: " << endl;
		int id;
		cin >> id;
		int index = this->IsExist(id);
		if (index == -1)
		{
			cout << "查无此人，修改失败！" << endl;
		}
		else
		{
			cout << "已找到编号为" << id << "的职工。" << endl;

			delete this->m_EmpArray[index];
			Worker* worker = NULL;

			int p_id;
			string p_name;
			int p_did;
			

			cout << "请输入职工的新编号: " << endl;
			cin >> p_id;
			cout << "请输入职工的姓名: " << endl;
			cin >> p_name;
			cout << "请输入职工的岗位: " << endl
				<< "1. 普通职工" << endl
				<< "2. 经理" << endl
				<< "3. 老板" << endl;
			cin >> p_id;
			switch (p_id)
			{
			case 1:
				worker = new Employee(p_id, p_name, p_id);
				break;

			case 2:
				worker = new Manager(p_id, p_name, p_id);
				break;

			case 3:
				worker = new Boss(p_id, p_name, p_id);
				break;
			default:
				break;
			}
			this->m_EmpArray[index] = worker;
			this->save();
			cout << "修改成功。" << endl;
		}

	}
	system("pause");
	system("cls");
}

void WorkerManager::Find_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件为空或是不存在。" << endl;
	}
	else
	{
		cout << "请选择查找方式: " << endl
			<< "1. 按照职工编号查找 " << endl
			<< "2. 按照职工姓名查找 " << endl;
		int find_way = 0;
		cin >> find_way;
		if (find_way == 1)	//按照职工编号查找
		{
			int p_id;
			cout << "请输入查找的职工编号: " << endl;
			cin >> p_id;
			int ret = this->IsExist(p_id);
			if (ret != -1)
			{	
				cout << "找到职工: " << endl;
				this->m_EmpArray[ret]->showInfo();
			}			
			else
			{
				cout << "查找失败，未找到该职工。" << endl;
			}
		}
		else if (find_way == 2) //按照职工姓名查找
		{
			int flag = true;
			string p_name;
			cout << "请输出查找职工姓名: " << endl;
			cin >> p_name;
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				if (p_name == this->m_EmpArray[i]->m_Name)
				{
					flag = false;
					cout << "找到职工: " << endl;
					this->m_EmpArray[i]->showInfo();
				}
			}
			if (flag)
			{
				cout << "查找失败，未找到该职工。" << endl;
			}
		}
		else
		{
			cout << "输入错误。" << endl;
		}
	}
	system("pause");
	system("cls");
}


void WorkerManager::Sort_Emp()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件夹不存在或为空。" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请选择排序方法: " << endl
			<< "1. 按职工编号升序排序" << endl
			<< "2. 按职工编号降序排序" << endl;
		int input = 0;
		cin >> input;
		if (input == 1 || input == 2)
		{
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				int m_index = i;
				for (int j = i; j < this->m_EmpNum; j++)
				{
					if(input == 1)	//升序
					{
						if (this->m_EmpArray[m_index]->m_ID > this->m_EmpArray[j]->m_ID)
						{
							m_index = j;
						}
					}
					else
					{
						if (this->m_EmpArray[m_index]->m_ID < this->m_EmpArray[j]->m_ID)
						{
							m_index = j;
						}
					}
				}

				if (i != m_index)
				{
					Worker* temp = this->m_EmpArray[i];
					this->m_EmpArray[i] = this->m_EmpArray[m_index];
					this->m_EmpArray[m_index] = temp;
				}
			}
			this->save();
			cout << "排序成功。排序结果为: " << endl;
			this->Show_Emp();
		}
		else
		{
			cout << "输入错误，排序失败。 " << endl;
			system("pause");
			system("cls");
		}
	}
}


void WorkerManager::Clear_File()
{
	cout << "是否确认清空文件: " << endl
		<< "1. 是" << endl
		<< "2. 否" << endl;
	int input = 0;
	cin >> input;
	if (input == 1)
	{
		//打开模式 ios::trunc 如果文件存在，则删除文件并重新创建空文件
		ofstream ofs(FILENAME, ios::trunc);
		ofs.close();

		if (this->m_EmpArray != NULL)
		{
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				if (this->m_EmpArray[i] != NULL)
				{
					delete this->m_EmpArray[i];
				}
			}
			this->m_EmpNum = 0;
			delete[] this->m_EmpArray;
		}
		cout << "文件已清空。" << endl;
	}

	system("pause");
	system("cls");
}


WorkerManager::	~WorkerManager()
{
	if (this->m_EmpArray != NULL)
	{
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			if (this->m_EmpArray[i] != NULL)
			{
				delete this->m_EmpArray[i];
			}
		}
		this->m_EmpNum = 0;
		delete[] this->m_EmpArray;
	}
}


